<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends MX_Controller {
  function __construct() {
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->model('product/mproduct');
    $this->load->library('parser');
    $this->load->library('form_validation');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->library('session');
    $this->cek();
     if($this->session->userdata('outlet')){
      $this->cek_actifity($this->session->userdata('outlet'));
    }
   
  }
	public function add_new($id = 0, $pesan = "hal"){
    $this->template->title('Sistem', "Product Edit");
    if($id > 0){
      $data_detail = $this->mproduct->get_detail($id);
//      $this->debug($data_detail, true);
    }
    else{
      $data_detail->name = "";
    }
    if(!$this->input->post(NULL, TRUE)){
      $parent = $this->global_models->get_dropdown("m_products", "id_products", "name", TRUE, array("parent" => 0));
      $this->template->build('add-new', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Product',
              'parent'  =>  $parent,
              'title_form'   => 'Product Form',
              'detail'  => array($data_detail),
              'foot'    => ''
            ));
      $this->template
        ->set_layout('default')
        ->build('add-new');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
            'name'            =>  $this->input->post("name", TRUE),
        );
        $hasil_in = $this->musers->update_user($this->input->post("id_detail", TRUE), $kirim);
        if($hasil_in)
          redirect ("users/index/list/sukses");
        else
          redirect ("users/index/list/filed");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
        $kirim = array(
            'name'            =>  $pst['name'],
            'parent'          =>  $pst['parent'],
            'code'            =>  $pst['code'],
            'create_by_users' =>  $this->session->userdata("id"),
            'create_date'     =>  date("Y-m-d")
        );
        $hasil_in = $this->global_models->insert("m_products", $kirim);
        if($hasil_in)
          redirect ("product/index/list/sukses");
        else
          redirect ("product/index/list/filed");
      }
    }
  }
  
  public function add_new_report($id = 0, $pesan = "hal") {
      $data_detail['bulan'] = array(
        '1' => 'Januari',
        '2' => 'Februari',
        '3' => 'Maret',
        '4' => 'April',
        '5' => 'Mei',
        '6' => 'Juni',
        '7' => 'Juli',
        '8' => 'Agustus',
        '9' => 'September',
        '10' => 'Oktober',
        '11' => 'November',
        '12' => 'Desember'
      );      
      
      $year_now = date('Y', strtotime('NOW'));
      $year_min_range = $year_now-1;
      $year_max_range = $year_now+1;
            
      $data_detail['tahun'] = array($year_min_range => $year_min_range, $year_now => $year_now, $year_max_range => $year_max_range);
            
      if(!$this->input->post(NULL, TRUE)){
      $this->template->build('add-new-report', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Report',
              'data'    => $data_detail,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
              'foot'    => '
                <script>
                    $(function () {                    
                    });
                </script>
                '
            ));
      $this->template
        ->set_layout('default')
        ->build('add-new-report');
    }
    else{
        $pst = $this->input->post(NULL, TRUE);
        if($pst['bulan']){  
          $laporan_detail = array(
              'month'   =>  $pst['bulan'],
              'year'    =>  $pst['tahun']
          );
                    
          //cek apakah laporan sudah ada atau belum
          $laporan = $this->global_models->get('m_actifity_stoct', $laporan_detail);
          
          if(!$laporan) {
            $kirim = array(
              "id_outlet"              =>  $this->session->userdata('outlet'),
              "name"                   =>  'Laporan '.$data_detail['bulan'][$pst['bulan']].' '.$pst['tahun'],
              "month"                  =>  $pst['bulan'],
              "day_in_month"           =>  cal_days_in_month(CAL_GREGORIAN, $pst['bulan'], $pst['tahun']),
              "year"                  =>  $pst['tahun'],
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
              "update_by_users"       =>  $this->session->userdata('id')
            );
            
            if($this->global_models->insert("m_actifity_stoct", $kirim))
                redirect ("product/daftar_laporan_stoct/sukses");
            else{
                redirect ("product/daftar_laporan_stoct/filed");
            }
          }
          else {
              redirect ("product/daftar_laporan_stoct/filed");
          }  
        }
        else{
          redirect ("product/index/list/filed");
        }
    }
  }
	public function stoct_masuk($id = 0, $pesan = "hal"){
    $this->template->title('Sistem', "Product Edit");
    if(!$this->input->post(NULL, TRUE)){
      $detail = $this->global_models->get("m_products", array("id_products" => $id));
      $this->template->build('stoct-masuk', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Stoct for '.$detail[0]->name,
              'title_form'   => 'Product Form',
              'detail'  => $detail,
              'foot'    => ''
            ));
      $this->template
        ->set_layout('default')
        ->build('stoct-masuk');
    }
    else{
      $pst = $this->input->post(NULL, TRUE);
      $kirim = array(
          'id_products'            =>  $pst['id_detail'],
          'date'          =>  $pst['tanggal'],
          'qty'            =>  $pst['qty'],
          'create_by_users' =>  $this->session->userdata("id"),
          'create_date'     =>  date("Y-m-d")
      );
      $hasil_in = $this->global_models->insert("d_stoct_gudang", $kirim);

      if($hasil_in)
        redirect ("product/stoct-utama/list/sukses");
      else
        redirect ("product/stoct-utama/list/filed");
    }
  }
	public function kirim_stoct($id = 0, $pesan = "hal"){
    $this->template->title('Sistem', "Product Edit");
    if(!$this->input->post(NULL, TRUE)){
      $detail = $this->global_models->get("m_products", array("id_products" => $id));
      $this->template->build('stoct-masuk', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Stoct for '.$detail[0]->name,
              'title_form'   => 'Product Form',
              'detail'  => $detail,
              'foot'    => ''
            ));
      $this->template
        ->set_layout('default')
        ->build('stoct-masuk');
    }
    else{
      $pst = $this->input->post(NULL, TRUE);
      $kirim = array(
          'id_products'            =>  $pst['id_detail'],
          'date'          =>  $pst['tanggal'],
          'qty'            =>  $pst['qty'],
          'create_by_users' =>  $this->session->userdata("id"),
          'create_date'     =>  date("Y-m-d")
      );
      $hasil_in = $this->global_models->insert("d_stoct_gudang", $kirim);

      if($hasil_in)
        redirect ("product/stoct-utama/list/sukses");
      else
        redirect ("product/stoct-utama/list/filed");
    }
  }
	public function delete($id){
    if($this->musers->change_status($id, 3))
      redirect("users/index/list/sukses");
    else
      redirect("users/index/list/filed");
  }
	public function status($id, $status){
    if($this->musers->change_status($id, $status))
      redirect("users/index/list/sukses");
    else
      redirect("users/index/list/filed");
  }
	public function export_xls(){
    $this->musers->export_xls("data-users");
  }
	public function index($action = "list", $pesan = "hal", $hal = 0){
    $list = $this->mproduct->get();
    $this->template->title('Sistem', "Product Master");
    
    $this->template->build('main', 
      array('message' => $pesan,
            'url'     => base_url()."themes/srabon/",
            'data'    => $list,
            'table_title'    => "Products Data Table",
//            'page'    => $this->pagination->create_links(),
            'foot'    => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('main');
	}
	public function stoct_utama($action = "list", $pesan = "hal", $hal = 0){
    $data = $this->global_models->get_query("
            SELECT A.*
            FROM m_products AS A
            WHERE A.parent != 0
            ");
//    $this->debug($data, true);
    $this->template->title('Sistem', "Product Master");
    
    $this->template->build('stoct-utama', 
      array('message' => $pesan,
            'url'     => base_url()."themes/srabon/",
            'data'    => $data,
            'table_title'    => "Products Data Table",
//            'page'    => $this->pagination->create_links(),
            'foot'    => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('stoct-utama');
	}
	public function distribusi_product($action = "list", $pesan = "hal", $hal = 0){
             $id = $this->session->userdata("id");
    //$data = $this->global_models->get("m_outlet");
    $data = $this->mproduct->get_milik($id);
//    $this->debug($data, true);
    $this->template->title('Sistem', "Product Master");
    
    $this->template->build('distribusi-product', 
      array('message' => $pesan,
            'url'     => base_url()."themes/srabon/",
            'data'    => $data,
            'table_title'    => "Products Data Table",
//            'page'    => $this->pagination->create_links(),
            'foot'    => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('distribusi-product');
	}
  function list_kirim_stoct($id_outlet){
    $data = $this->global_models->get("d_outlet_pengiriman", array("id_outlet"  =>  $id_outlet));
//    $this->debug($data, true);
    $this->template->title('Sistem', "Product Master");
    
    $this->template->build('list-kirim-stoct', 
      array('message' => "",
            'url'     => base_url()."themes/srabon/",
            'data'    => $data,
            'table_title'    => "Tanggal Pengiriman Product",
            'id_outlet'    => $id_outlet,
            'foot'    => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('list-kirim-stoct');
  }
  function master_pengiriman($id_outlet, $id_outlet_pengiriman = 0, $pesan = ""){
    $this->template->title('Sistem', "Product Edit");
    if(!$this->input->post(NULL, TRUE)){
      $detail = $this->global_models->get("m_outlet", array("id_outlet" => $id_outlet));
      $product = $this->global_models->get("m_products", array("parent" => 0));
      foreach ($product as $prdk) {
        $hasil[$prdk->id_products] = array("nama" => $prdk->name, "child" => $this->global_models->get("m_products", array("parent" => $prdk->id_products)));
      }
      if($id_outlet_pengiriman > 0){
        $d_outlet_pengiriman = $this->global_models->get("d_outlet_pengiriman", array("id_outlet_pengiriman" => $id_outlet_pengiriman));
        $data_pro = $this->global_models->get("d_product_pengiriman", array("id_outlet_pengiriman" => $id_outlet_pengiriman, "tanggal" => 0));
      }
      $this->template->build('master-pengiriman', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Products yang Dikirim untuk '.$detail[0]->name,
              'title_form'   => 'Product Form',
              'detail'  => $detail,
              'product'  => $hasil,
              'data_pro'  => $data_pro,
              'd_outlet_pengiriman'  => $d_outlet_pengiriman,
              'id_outlet'  => $id_outlet,
              'foot'    => ''
            ));
      $this->template
        ->set_layout('default')
        ->build('master-pengiriman');
    }
    else{
      $pst = $this->input->post(NULL, TRUE);
      $kirim = array(
          'id_outlet'            =>  $pst['id_outlet'],
          'pic'          =>  $pst['pic'],
          'tanggal'            =>  $pst['tanggal'],
          'id_users'            =>  $this->session->userdata("id"),
          'create_by_users' =>  $this->session->userdata("id"),
          'create_date'     =>  date("Y-m-d")
      );
      $hasil_in = $this->global_models->insert("d_outlet_pengiriman", $kirim);
      $product = $this->global_models->get("m_products", array("parent > " => 0));
      if($hasil_in){
        foreach ($product as $value) {
          $qty = 0;
          if($pst['child_'.$value->id_products])
                  $qty = $pst['child_'.$value->id_products];
          
          $data_product = array(
              "id_outlet"   =>  $pst['id_outlet'],
              "id_products" =>  $value->id_products,
              "qty"         =>  $qty,
              "id_outlet_pengiriman"  =>  $hasil_in
          );
          $hasil_product = $this->global_models->insert("d_product_pengiriman", $data_product);
          $pst = $this->input->post(NULL, TRUE);
          $kirim = array(
              'id_products'            =>  $value->id_products,
              'date'          =>  $pst['tanggal'],
              'qty'            =>  "-".$qty,
              'create_by_users' =>  $this->session->userdata("id"),
              'create_date'     =>  date("Y-m-d")
          );
          $hasil_in_stoct = $this->global_models->insert("d_stoct_gudang", $kirim);
        }
      }

      if($hasil_in)
        redirect ("product/list-kirim-stoct/{$pst['id_outlet']}/sukses");
      else
        redirect ("product/list-kirim-stoct/{$pst['id_outlet']}/filed");
    }
  }
  function daftar_laporan_stoct(){
    if($this->session->userdata("outlet") <= 0){
      redirect("outlet/milik");
    }
    $list = $this->global_models->get_query("
      SELECT A.*, B.name as outlet
      FROM m_actifity_stoct AS A
      LEFT JOIN m_outlet AS B ON A.id_outlet = B.id_outlet
      WHERE A.id_outlet = {$this->session->userdata('outlet')}
      ORDER BY A.year, A.month DESC
      ");
    $this->template->title('Sistem', "Outlet");
    
    $view = "aktifitas-daftar";
//    if($this->mobile_detect->isMobile()){
//      $view = "mobile/m-aktifitas-daftar";
//    }
    
    $this->template->build($view, 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'data'    => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "outlet",
            'title_table'    => "Data Laporan Bualanan Stoct ".$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $this->session->userdata('outlet'))),
            'foot'    => ""
          ));
    $this->template
      ->set_layout('default')
      ->build($view);
  }
  public function detail_stoct($id){
    $newdata = array(
      'outlet'  => $id
    );
    $this->session->set_userdata($newdata);
    redirect("product/daftar-laporan-stoct");
  }
  private function cek_actifity($id){
    if($this->global_models->get_field("m_actifity_stoct", "id_actifity_stoct", 
            array("month"  =>  date("m"), "year" => date("Y"), "id_outlet" => $id)) === FALSE){
      $this->global_models->insert("m_actifity_stoct", 
              array(
                  "id_outlet" =>  $id,
                  "name"      =>  "Laporan ".date("F")." ".date("Y"),
                  "month"     =>  date("m"),
                  "day_in_month"     =>  date("t"),
                  "year"      =>  date("Y"),
                  "create_by_users" =>  1,
                  "create_date" =>  date("Y-m-d H:i:s"),
                  "update_by_users" =>  1
                  ));
    }
    return true;
  }
  function laporan_detail($id, $day = 0){
    $detail = $this->global_models->get("m_actifity_stoct", array("id_actifity_stoct"  =>  $id));
    $master_products = $this->global_models->get("m_products", array("parent"  =>  0));
    foreach($master_products as $mp){
      $product[$mp->id_products] = array(
          "name"    =>  $mp->name,
          "child"   =>  $this->global_models->get_query("
            SELECT A.*, SUM(B.qty) AS qty, SUM(B.qty_harian) AS qty_harian
            FROM m_products AS A
            JOIN d_product_pengiriman AS B ON A.id_products = B.id_products
            WHERE B.id_outlet = {$detail[0]->id_outlet}
              AND A.parent = {$mp->id_products}
            GROUP BY B.id_products
            ")
      );
    }
    $child_product = $this->global_models->get("m_products", array("parent > " => 0));
    for($i = 1; $i <= $detail[0]->day_in_month; $i++){
      foreach($child_product as $cp){
        $data[$cp->id_products][$i] = -1 * $this->global_models->get_field("d_product_pengiriman", "qty_harian", 
                array(
                    "tanggal"  =>  $i, 
                    "bulan" =>  $detail[0]->month, 
                    "tahun"  =>  $detail[0]->year, 
                    "id_products" => $cp->id_products,
                    "id_outlet" =>  $detail[0]->id_outlet
                ));
      }
    }
    $bulan = array(
        "1" =>  "Januari",
        "2" =>  "Februari",
        "3" =>  "Maret",
        "4" =>  "April",
        "5" =>  "Mei",
        "6" =>  "Juni",
        "7" =>  "Juli",
        "8" =>  "Agustus",
        "9" =>  "September",
        "10" =>  "Oktober",
        "11" =>  "Nopember",
        "12" =>  "Desember",
    );
//    $this->debug($detail, true);
    $this->template->build("aktifitas-detail", 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'detail'    => $detail,
            'product'    => $product,
            'data'    => $data,
            'menu_drop'   => $this->menu,
            'menu'        => "outlet",
            'title_table'    => "Laporan Harian ".$bulan[$detail[0]->month]." ".$detail[0]->year." Outlet ".$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $detail[0]->id_outlet)),
            'foot'    => "",
            'privilege'    => "aktifitas_editable",
            'day_mobile'    => $day,
          ));
    $this->template
      ->set_layout('default')
      ->build("aktifitas-detail");
  }
  function simpan_product($id, $day, $id_outlet, $month, $year){
    $angka = $this->input->post('value', true);
    $angka = str_replace(".", "", $angka);
    $angka = str_replace(",", ".", $angka);
    
    $this->global_models->insert("d_product_pengiriman", array(
        "id_outlet"   =>  $id_outlet,
        "id_products" =>  $id,
        "tanggal"     =>  $day,
        "bulan"       =>  $month,
        "tahun"       =>  $year,
        "qty_harian"         =>  "-".$angka
    ));
    
    if($angka > 0)
      print number_format($angka, 0, ",", ".");
    else
      print 0;
    die;
  }
  function ajax_tanggal(){
    $id_self = $this->global_models->get_field("d_product_pengiriman", "id_product_pengiriman", 
            array(
                "id_outlet" => $this->input->post("id_outlet"),
                "id_products" => $this->input->post("id_products"),
                "tanggal" => $this->input->post("tanggal"),
                "bulan" => $this->input->post("bulan"),
                "tahun" => $this->input->post("tahun"),
                ));
    if($id_self > 0){
      $this->global_models->update("d_product_pengiriman", array("id_product_pengiriman" => $id_self), array("qty_harian" => "-".$this->input->post("value")));
    }
    else{
      $this->global_models->insert("d_product_pengiriman", 
              array(
                  "qty_harian"  =>  "-".$this->input->post("value"), 
                  "tahun"       =>  $this->input->post("tahun"),
                  "bulan"       =>  $this->input->post("bulan"),
                  "tanggal"     =>  $this->input->post("tanggal"),
                  "id_products" =>  $this->input->post("id_products"),
                  "id_outlet"   =>  $this->input->post("id_outlet"),
                  ));
    }
//    $this->debug($id_self, true);
    die;
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */